23.5 Application Events and Listeners(应用事件和监听)
1 | In addition to the usual Spring Framework events, such as |
除了通常的Spring框架事件(如ContextRefreshedEvent)之外,SpringApplication还会发送一些其他应用程序事件。1
2
3
4
5
6
7
8
9
10Some events are actually triggered before the ApplicationContext is
created, so you cannot register a listener on those as a @Bean. You
can register them with the SpringApplication.addListeners(… ) method or
the SpringApplicationBuilder.listeners(… ) method.
If you want those listeners to be registered automatically, regardless
of the way the application is created, you can add a
META-INF/spring.factories file to your project and reference your
listener(s) by using the org.springframework.context.ApplicationListener
key, as shown in the following example:
org.springframework.context.ApplicationListener=com.example.project.MyListener
一些事件其实是在应用上下文创建之前就已经创建了,所以您不能够通过@Bean给这些事件增加监听。您可以通过SpringApplication.addListeners(… )方法或者SpringApplicationBuilder.listeners(… )方法来给这些事件注册监听。
如果您想这些事件自动注册,而不管应用是否被创建,您可以通过增加META-INF/spring.factories文件到您的项目中,然后通过org.springframework.context.ApplicationListener作为key关联您的监听,如下例子:org.springframework.context.ApplicationListener=com.example.project.MyListener1
Application events are sent in the following order, as your application runs:
随着您的应用程序运行,应用程序事件按以下顺序发送:1
2
3
4
5
61. An ApplicationStartingEvent is sent at the start of a run but before any processing, except for the registration of listeners and initializers.
2. An ApplicationEnvironmentPreparedEvent is sent when the Environment to be used in the context is known but before the context is created.
3. An ApplicationPreparedEvent is sent just before the refresh is started but after bean definitions have been loaded.
4. An ApplicationStartedEvent is sent after the context has been refreshed but before any application and command-line runners have been called.
5. An ApplicationReadyEvent is sent after any application and command-line runners have been called. It indicates that the application is ready to service requests.
6. An ApplicationFailedEvent is sent if there is an exception on startup.
- 1.ApplicationStartingEvent 会在程序注册完监听器和初始器之后,其他所有进程运行之前发送。
- 2.ApplicationEnvironmentPreparedEvent 在Environment被使用之后,容器被创建之前发送。
- 3.ApplicationPreparedEvent在刷新启动之前,但在加载了bean定义之后发送。
- 4.ApplicationStartedEvent在上下文被刷新之后,任何应用和command-line运行之前将被调用
- 5.ApplicationReadyEvent在刷新启动之后被发送,并且处理了任何相关的回调以指示应用程序准备好服务请求。
- 6.ApplicationFailedEvent如果启动时发生异常发送。
1 | You often need not use application events, but it can be handy to know |
您经常不需要使用应用程序事件,但可以方便地知道它们存在。在内部,Spring Boot使用事件来处理各种任务。
1 | Application events are sent by using Spring Framework’s event |
应用事件发送是采用的Spring框架的事件发布机制。该机制的一部分确保发布给子上下文中侦听器的事件也会发布给任何祖先上下文中的侦听器。因此,如果您的应用使用的是SpringApplication级联结构实例化,监听器可以接收同一类型的应用程序事件的多个实例。1
2
3
4
5
6To allow your listener to distinguish between an event for its context
and an event for a descendant context, it should request that its
application context is injected and then compare the injected context
with the context of the event. The context can be injected by
implementing ApplicationContextAware or, if the listener is a bean,
by using @Autowired.
允许您的侦听器区分其上下文的事件和后代上下文的事件,它应该请求注入它应用的上下文,并比较注入上线文的事件。上线文可以通过实现ApplicationContextAware接口注入,或者如果监听是个bean可以通过@Autowired来注入